package com.worldcup.service.help;

import com.worldcup.model.Travel;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author duwan.zq
 * @date 2018/05/23
 */
@Component
public class VehicleMatch implements TravelMatch{

    private final static Logger logger = LoggerFactory.getLogger(VehicleMatch.class);

    private final static double THRESHOLD = 0.5;

    @Override
    public boolean isMatch(List<Travel> travels, List<Travel> refTravels) {
        if(CollectionUtils.isEmpty(travels)){
            return false;
        }
        if(CollectionUtils.isEmpty(refTravels)){
            return false;
        }
        for(Travel travel :travels){
            String vehicleNo = travel.getVehicleNo();
            if(StringUtils.isBlank(vehicleNo)){
                continue;
            }
            for(Travel refTravel : refTravels){
                String refVehicleNo = refTravel.getVehicleNo();
                if(StringUtils.isBlank(refVehicleNo)){
                    continue;
                }
                if(com.worldcup.utils.StringUtils.getLevenshteinDistance(vehicleNo, refVehicleNo) >= THRESHOLD){
                    return true;
                }
            }
        }
        return false;
    }
}
